package com.dycx.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.VehicleTypeConverter;
import com.dycx.user.dto.VehicleTypeDTO;
import com.dycx.user.entity.VehicleTypeDO;
import com.dycx.user.mapper.VehicleTypeMapper;
import com.dycx.user.service.VehicleTypeService;
import com.dycx.user.vo.VehicleTypeVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@AllArgsConstructor
public class VehicleTypeServiceImpl extends ServiceImpl<VehicleTypeMapper, VehicleTypeDO> implements VehicleTypeService {

    private final VehicleTypeMapper vehicleTypeMapper;

    @Override
    public Boolean delete(Integer id) {
        return this.removeById(id);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean insert(VehicleTypeDTO dto) {
        if (this.getInfoByVehicleTypeName(dto.getVehicleTypeName()) != null) {
            throw new RuntimeException("已存在相同车型名称，请勿重复添加");
        }
        return this.save(VehicleTypeConverter.dtoToEntity(dto));
    }

    /**
     * 添加/修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean update(VehicleTypeDTO dto) {
        return this.updateById(VehicleTypeConverter.dtoToEntity(dto));
    }

    /**
     * 更新排序
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean updateSort(VehicleTypeDTO dto) {
        VehicleTypeDO carType = new VehicleTypeDO();
        carType.setId(dto.getId());
        carType.setSort(dto.getSort());
        return this.updateById(carType);
    }

    /**
     * 更新状态
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean updateStatus(VehicleTypeDTO dto) {
        VehicleTypeDO carType = new VehicleTypeDO();
        carType.setId(dto.getId());
        carType.setStatus(dto.getStatus());
        return this.updateById(carType);
    }

    @Override
    public VehicleTypeVO getInfoById(Integer id) {
        return vehicleTypeMapper.getInfoById(id);
    }

    @Override
    public VehicleTypeVO getInfoByVehicleTypeName(String vehicleTypeName) {
        return vehicleTypeMapper.getInfoByCarTypeName(vehicleTypeName);
    }

    @Override
    public List<VehicleTypeVO> getList(List<Integer> vehicleTypeIds) {
        LambdaQueryWrapper<VehicleTypeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(VehicleTypeDO::getSort);
        queryWrapper.eq(VehicleTypeDO::getStatus, true);
        if (ObjectUtil.isNotEmpty(vehicleTypeIds)) {
            queryWrapper.in(VehicleTypeDO::getId, vehicleTypeIds);
        }

        return VehicleTypeConverter.entityToVoList(this.list(queryWrapper));
    }

    @Override
    public Page<VehicleTypeVO> getPage(VehicleTypeDTO dto) {
        Page<VehicleTypeVO> page = new Page<>(dto.getPageIndex(), dto.getPageSize());  // 查询第1页，每页返回5条
        return vehicleTypeMapper.getPage(page, dto);
    }

    /**
     * 获取多个车型名称
     *
     * @param vehicleTypeIds
     * @param list
     * @return
     */
    @Override
    public List<String> getVehicleTypeName(List<Integer> vehicleTypeIds, List<VehicleTypeVO> list) {
        List<String> al = new ArrayList<>();

        if (ObjectUtil.isEmpty(vehicleTypeIds) || ObjectUtil.isEmpty(list)) {
            return al;
        }
        list.forEach(item -> {
            if (vehicleTypeIds.contains(item.getId())) {
                al.add(item.getVehicleTypeName());
            }
        });

        return al;
    }
}
